﻿using Gp.GameLogic;
using Gp.Scripts.Core;
using QFramework;
using UnityEngine;
using UnityEngine.UI;

namespace Gp.Scripts.Character {
    public class Conversable : InteractableItem {
        public string NpcName => info.name;
        public EntityConfigInfo info;


        // private InteractableDrawerWrapper _tipDrawerWrapper;
        // private bool IsCollision { get => _tipDrawerWrapper.IsInteractable; set => _tipDrawerWrapper.IsInteractable = value; }
        [SerializeField] private Text nameText;
        [SerializeField] private GameObject tooltipPivot;

        public override Transform DrawerSource => tooltipPivot.transform;

        private void Reset() {
            if (tooltipPivot != null) return;

            var obj = new GameObject("Tooltips Pivot");
            obj.transform.SetParent(this.transform);
            tooltipPivot = obj;
            tooltipPivot.transform.LocalPositionIdentity();

            const float heightOffset = .5f;


            if (TryGetComponent<Collider>(out var col)) {
                var height = col.bounds.max.y;
                tooltipPivot.transform.PositionY(height + heightOffset);
            }
        }


        protected override void OnInit() {
            if (nameText != null) nameText.text = GetDisplayName();
            // AddInfoEntry(new InteractEntryInfo(() => "交谈：" + GetDisplayName(), OnInteract));
        }


        private string GetDisplayName() {
            var item = Global.Get<EntitySystem>().GetCharacterInfo(NpcName);
            if (item == null) return "";
            return item.displayName.IsNullOrEmpty() ? item.characterName : item.displayName;
        }


        private void OnInteract() {
            Global.Get<ConversationSystem>().StartConversation(NpcName);
        }
        
        //
        //
        // private void OnTriggerEnter(Collider other) {
        //     if (!other.gameObject.CompareTag(PlayerConst.PlayerTag)) {
        //         return;
        //     }
        //     MainArchitecture.Interface.SendEvent(new ShowInteractionKeyArgs(InputMapCode.Interact));
        //     IsCollision = true;
        // }
        //
        //
        //
        // private void OnTriggerExit(Collider other) {
        //     if (!other.gameObject.CompareTag(PlayerConst.PlayerTag)) {
        //         return;
        //     }
        //     
        //     MainArchitecture.Interface.SendEvent(new HideInteractionKeyArgs());
        //     IsCollision = false;
        // }
    }
}